package ru.lenwp.operation.admin;

import ru.lenwp.context.Context;
import ru.lenwp.model.OperationInfo;
import ru.lenwp.model.User;
import ru.lenwp.operation.Operation;

import java.util.Iterator;
import java.util.List;

import static ru.lenwp.StringConstants.*;
import static ru.lenwp.StringConstants.nameValueDelimiter;

/**
 * Created by Lenar on 16.07.2014.
 */
public class ShowUsersOperation implements Operation {
    private final Context context;

    public ShowUsersOperation(Context context) {
        this.context = context;
    }

    @Override
    public OperationInfo execute() {
        printAllUsers(context.getUserDao().allUser());
        return null;
    }

    public void printAllUsers(List<User> userList) {
        Iterator<User> userIterator = userList.iterator();
        while (userIterator.hasNext()) {
            User currentUser = userIterator.next();
            StringBuilder lineBuilder = new StringBuilder();
            lineBuilder.append(FIRST_NAME_CAPTION).append(nameValueDelimiter).append(currentUser.getFirstName())
                    .append(LAST_NAME_CAPTION).append(nameValueDelimiter).append(currentUser.getLastName()).
                    append(EMAIL_CAPTION).append(nameValueDelimiter).append(currentUser.getEmail());
            System.out.println(lineBuilder.toString());
        }
    }

    @Override
    public String getOperationName() {
        return "Список пользователей";
    }
}

